package com.example.j280_webproject.servlet;

import com.example.j280_webproject.day036.JDBC01_delete;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/delete")
public class DeleteServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        int i = 0;
        String id = req.getParameter("id"); // 取到的null
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();

        if(id==null){
            writer.print("对不起，请输入id值");
            return;
        }
        try {
            i = Integer.parseInt(id);
        } catch (NumberFormatException e) {
            writer.print("你傻嘛？请输入纯数字的id值");
            return;
        }
        JDBC01_delete jdbc01_delete = new JDBC01_delete();
        try {
            jdbc01_delete.delete(i);
            writer.print("删除数据成功！");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
            writer.print("删除数据失败，系统繁忙！");
        }

    }
}
